//
// Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
//
// GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
//  Author: Shawn.Tan
//  Date : 2025.10.28
//
//  History : Initial Version 2025.10.28
//

//
#include "UtilityFunctionsGPGPU.h"

#include <string>

#include "Log.h"

using namespace std;

namespace Green {

  void privilege_prefix(std::string& rStr, uint32 priv, bool upper)
  {
    privilege_prefix(rStr, EPrivilegeLevelType(priv), upper);
  }

  void privilege_prefix(std::string& rStr, EPrivilegeLevelType priv, bool upper)
  {
    switch (priv)
    {
      case EPrivilegeLevelType::M:
        rStr = (upper) ? "M" : "m";
        break;
      case EPrivilegeLevelType::H:
        rStr = (upper) ? "H" : "h";
        break;
      case EPrivilegeLevelType::S:
        rStr = (upper) ? "S" : "s";
        break;
      case EPrivilegeLevelType::U:
        rStr = (upper) ? "U" : "u";
        break;
      default:
        LOG(warn) << "{UtilityFunctionsGPGPU::privilege_register_prefix} unknown priv level type=" << EPrivilegeLevelType_to_string(priv) << endl;
        break;
    }
  }

  bool illegal_exception_return(uint32 targetPriv, uint32 currentPriv)
  {
    if (targetPriv > currentPriv) return true;

    return false;
  }

  EPagingExceptionType get_exception_type_from_access_type(bool isInstr, EMemAccessType memAccess)
  {
    if (isInstr)
    {
      return EPagingExceptionType::InstructionPageFault;
    }

    if (memAccess == EMemAccessType::ReadWrite or memAccess == EMemAccessType::Write)
    {
      return EPagingExceptionType::StoreAmoPageFault;
    }

    return EPagingExceptionType::LoadPageFault;
  }
}
